package com.bw.common.mvvm.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.bw.common.BaseApplication;
import com.bw.common.field.ActionEntity;
import com.bw.common.field.UIBusAction;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import java.lang.ref.WeakReference;
import java.lang.reflect.ParameterizedType;

public abstract class BaseFragment <V extends ViewDataBinding,VM extends BaseViewModel>
        extends Fragment implements IView {

    protected V v;
    protected VM vm;
    private BaseFragment.UIObsever obsever;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //沉浸式状态栏导入
        createViewModel();
        obsever = new BaseFragment.UIObsever(this);
        //databing创建
        v = DataBindingUtil.inflate(inflater,bindlayout(),container,false);
        v.setVariable(createvariableId(),vm);
        //将vm设置到databinding
        getLifecycle().addObserver(vm);
        vm.getUiLiveDataBus().getUiLiveDataBus().observe(this,obsever);
        return v.getRoot();
    }


    protected abstract int createvariableId();

    @Override
    public void onDestroy() {
        super.onDestroy();
        vm.getUiLiveDataBus().getUiLiveDataBus().removeObserver(obsever);
        getLifecycle().removeObserver(vm);
        v.unbind();
    }

    @Override
    public void createViewModel() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) parameterizedType.getActualTypeArguments()[1];
        vm = (VM) new ViewModelProvider(this,
                new ViewModelProvider.NewInstanceFactory()).get(clazz);
    }

    @Override
    public void startPage(Class clazz) {
        startActivity(new Intent(getActivity(),clazz));
    }

    //Bundle->传参大小限制为1mb
    @Override
    public void startPage(Class clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(),clazz);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void finishPage() {
        getActivity().finish();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void disDialog() {

    }

    @Override
    public void showErro(String msg) {
        Toast.makeText(BaseApplication.getInstance(),msg,Toast.LENGTH_SHORT).show();
    }

    public static class UIObsever implements Observer<UIBusAction> {

        private WeakReference<BaseFragment> weakReference;

        public UIObsever(BaseFragment activity){
            weakReference = new WeakReference<>(activity);
        }

        @Override
        public void onChanged(UIBusAction uiBusAction) {
            if (weakReference.get() == null)
                return;
            if (uiBusAction.action.equals(ActionEntity.SHOWERRO)){
                String str = (String) uiBusAction.parames.get(ActionEntity.ERROKEY);
                weakReference.get().showErro(str);
                return;
            }
            if (uiBusAction.action.equals(ActionEntity.STARTPAGE)){
                Class clazz = (Class) uiBusAction.parames.get(ActionEntity.CLASSKEY);
                weakReference.get().startPage(clazz);
                return;
            }
            if (uiBusAction.action.equals(ActionEntity.STARTBUNDLEPAGE)){
                Class clazz = (Class) uiBusAction.parames.get(ActionEntity.CLASSKEY);
                Bundle bundle = (Bundle) uiBusAction.parames.get(ActionEntity.BUNDLEKEY);
                weakReference.get().startPage(clazz,bundle);
                return;
            }
            if (uiBusAction.action.equals(ActionEntity.FINISHPAGE)){
                weakReference.get().finishPage();
                return;
            }
            if (uiBusAction.action.equals(ActionEntity.SHOWDIALOG)){
                weakReference.get().showDialog();
                return;
            }
            if (uiBusAction.action.equals(ActionEntity.DISDIALOG)){
                weakReference.get().disDialog();
                return;
            }
        }
    }

}
